using UnityEngine;

public class GameSetup : MonoBehaviour
{
    [Header("Player Settings")]
    public GameObject playerPrefab;
    
    [Header("Camera Settings")]
    public GameObject cameraPrefab;
    
    [Header("Room Settings")]
    public RoomGenerator roomGenerator;

    private void Start()
    {
        SetupPlayer();
        SetupCamera();
        SetupRooms();
    }

    private void SetupPlayer()
    {
        if (playerPrefab != null)
        {
            GameObject player = Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
            
            // Ensure required components are attached
            if (player.GetComponent<PlayerController>() == null)
                player.AddComponent<PlayerController>();
            if (player.GetComponent<PlayerStats>() == null)
                player.AddComponent<PlayerStats>();
            if (player.GetComponent<PlayerInventory>() == null)
                player.AddComponent<PlayerInventory>();
            if (player.GetComponent<SkillSystem>() == null)
                player.AddComponent<SkillSystem>();
                
            player.tag = "Player";
        }
    }

    private void SetupCamera()
    {
        if (cameraPrefab != null)
        {
            GameObject camera = Instantiate(cameraPrefab);
            ThirdPersonCamera thirdPersonCamera = camera.GetComponent<ThirdPersonCamera>();
            if (thirdPersonCamera != null)
            {
                GameObject player = GameObject.FindGameObjectWithTag("Player");
                if (player != null)
                {
                    thirdPersonCamera.target = player.transform;
                }
            }
        }
    }

    private void SetupRooms()
    {
        if (roomGenerator != null)
        {
            Floor floor = roomGenerator.GenerateFloor(1, 1);
            Debug.Log($"Generated floor with {floor.GetAllRooms().Count} rooms");
        }
    }
}